--[[
	
]]
local filename = "table.lua"

local skynet = require "skynet"
local cluster = require "skynet.cluster"
local timetool = require "timetool"
local filelog = require "filelog"
local routerbase = require "routerbase"

require "imenum"

local json = require "cjson"
json.encode_sparse_array(true, 1, 1)

--
--
local Talk = routerbase:new({
	LOGICPOOL = {},
	talker_data = {
		talk_type = 0, 				---客户端类型(1 玩家端 2 客服端)
		state = 0,	                --状态
        rid = 0,
        playerinfo = nil,
		uuid = "",                  --唯一id
        tradeid = 0,                --节点唯一id
		index = 0,  	            
		last_heart_time = 0, 		--
		sitdown_player_num = 0,     --坐下的玩家数
		last_send_time = 0,
		conf = nil,                 --桌子配置（来自客户端）
		gamelogic = nil,            --游戏逻辑对象
		timer_id = -1,              --通用计时器
		isdeleting = false,           --是否被标示为可删除
		delete_talk_timer_id = -1,
		is_disconnected = false,
	},
})


--
-- 初始化数据
--
-- 注意这里的顺序要先 add_logic ,然后再 add_func
-- 因为 func里的init 有会使用 logic 里的定义，所以要先add_loginc
--
function Talk:init()
	self:add_logic("talklogic", "talkmsg/talklogic")  -- 桌子逻辑
	-- 放到CMD{} 里面执行了
	self:add_func("talkmsg/talkcmd")
	self:add_func("talkmsg/talkrequest")
	self:add_func("talkmsg/talktimer")

	--[[
	skynet.fork(function() 
			while true do
				local nowtime = timetool.get_time()
				if self.talker_data.last_heart_time > 0 and nowtime - self.talker_data.last_heart_time > 20 then
					skynet.send(".router","lua","leave_talk", nil, self.talker_data.rid, self.talker_data.index)
					self:exit_service()
				end
				skynet.sleep(2 * 100)
			end
		end)
	--]]
end

--
--添加逻辑处理模块
--
function Talk:add_logic(name, logicfile)
	self.LOGICPOOL[name] = require(logicfile)
    if self.LOGICPOOL[name] then
	    if self.LOGICPOOL[name].init and type(self.LOGICPOOL[name].init) == "function" then
		    self.LOGICPOOL[name].init(self)
	    end
    else
        filelog.sys_error("add logic failed!",name,logicfile)
    end
end

--
--通过logic_name，获得逻辑对象
--
function Talk:get_logicbyname(logic_name)
	return self.LOGICPOOL[logic_name]
end

--
--向列表中玩家发通知消息
--is_new_format 是否新格式
--
function Talk:send_notice_to_players(rids, msgname, message, retcode, retstr, is_new_format)
    local status = true
    if is_new_format then
        local tmp = { msgname = msgname, msgbody = json.encode(message)}
        status = skynet.pcall(cluster.send,"gatenode", ".router", "notify", rids, "im.".. "notifyN", tmp, retcode, retstr)
    else
	    status = skynet.pcall(cluster.send,"gatenode", ".router", "notify", rids, "im."..msgname, message, retcode, retstr)
    end
    if not status then
        filelog.sys_error("notice player failed!")
    end
end

--
--  通知gate
--
function Talk:notice_tableinfo_togatenode(msgname, rid, gamenode, table_index)
	local status = skynet.pcall(cluster.send,"gatenode", ".router", msgname, rid, gamenode, table_index)
    if not status then
        filelog.sys_error("send msg to gate error!")
    end
end

--
--  结束服务
--
function Talk:exit_service()
	skynet.exit()
end

--
-- lua消息处理
--
function Talk:process(command, ...)
	skynet.call(skynet.self(), "lua", command, ...)
end

skynet.start(function()
	Talk:start()
end)